<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateSelectionAPIRequest;
use App\Http\Requests\API\UpdateSelectionAPIRequest;
use App\Models\Selection;
use App\Repositories\SelectionRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class SelectionController
 * @package App\Http\Controllers\API
 */

class SelectionAPIController extends AppBaseController
{
    /** @var  SelectionRepository */
    private $selectionRepository;

    public function __construct(SelectionRepository $selectionRepo)
    {
        $this->selectionRepository = $selectionRepo;
    }

    /**
     * Display a listing of the Selection.
     * GET|HEAD /selections
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->selectionRepository->pushCriteria(new RequestCriteria($request));
        $this->selectionRepository->pushCriteria(new LimitOffsetCriteria($request));
        if($request->get('type')==1){
            $selections = $this->selectionRepository->all();
        }else{
            $selections = $this->selectionRepository->all()->toTree();
        }


        return $this->sendResponse($selections->toArray(), '配置列表获取成功');
    }

    /**
     * Store a newly created Selection in storage.
     * POST /selections
     *
     * @param CreateSelectionAPIRequest $request
     *
     * @return Response
     */
    public function store(CreateSelectionAPIRequest $request)
    {
        $input = $request->all();
        $selections = $this->selectionRepository->create($input);
        $parent = Selection::find($request->get('parent_id',0));

        if($parent) {
            $selections->appendToNode($parent)->save();
        }

        return $this->sendResponse($selections->toArray(), '数据添加成功');
    }


    /**
     * Update the specified Selection in storage.
     * PUT/PATCH /selections/{id}
     *
     * @param  int $id
     * @param UpdateSelectionAPIRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateSelectionAPIRequest $request)
    {
        $input = $request->all();

        /** @var Selection $selection */
        $selection = $this->selectionRepository->findWithoutFail($id);

        if (empty($selection)) {
            return $this->sendError('数据不存在');
        }
        //查询父节点
        $parent = Selection::find($request->get('parent_id',0));

        $selection = $this->selectionRepository->update($input, $id);

        if($parent) {
            $selection->appendToNode($parent)->save();
        }

        return $this->sendResponse($selection->toArray(), '数据修改成功');
    }

    /**
     * Remove the specified Selection from storage.
     * DELETE /selections/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        /** @var Selection $selection */
        $selection = $this->selectionRepository->findWithoutFail($id);

        if (empty($selection)) {
            return $this->sendError('数据不存在');
        }

        $selection->delete();

        return $this->sendResponse($id, '数据删除成功');
    }
}
